//
//  TopViewController.m
//  HuiWenMovie
//
//  Created by niceDay on 15/7/17.
//  Copyright (c) 2015年 computer. All rights reserved.
//

#import "TopViewController.h"
#import "topMovieMesViewController.h"
@interface TopViewController ()

@end

@implementation TopViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.title =@"Top250";    }
    return self;

}


- (void)viewDidLoad {
    [super viewDidLoad];
 //   self.view.backgroundColor = [UIColor blackColor];
    [self _createCollectionView];
    [self _initData];
    [self _loadData];
      // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark- createSubviews
-(void)_createCollectionView{
    self.navigationController.navigationBar.translucent = YES;
    self.tabBarController.tabBar.translucent = YES;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(Kwidth/3-5, 180);
    layout.minimumInteritemSpacing = 2.5;
    layout.minimumLineSpacing = 20;
    
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collection.dataSource = self;
    collection.delegate =self;
    
    UINib *nib = [UINib nibWithNibName:@"TopCell" bundle:[NSBundle mainBundle]];
    [collection registerNib:nib forCellWithReuseIdentifier:@"cell"];
    
    [self.view addSubview:collection];
}

#pragma mark - initData

- (void)_initData{
    _modalArray = [[NSMutableArray alloc] init];
}

#pragma mark - loadData
- (void)_loadData{
    NSDictionary *dic = [DataService initWitnJsionFileName:Top250Json];
    NSArray *subjects = [dic objectForKey:@"subjects"];
    for (NSDictionary *contentDic in subjects) {
        TopModal *modal = [[TopModal alloc] init];
        
        [modal setValuesForKeysWithDictionary:contentDic];
//        modal.average = [contentDic[@"rating"][@"average"] floatValue];
//        modal.title = contentDic[@"title"];
//        modal.images = contentDic[@"images"];
        [_modalArray addObject:modal];
    }

}

#pragma mark - delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  [_modalArray count];
    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    TopModal *modal = _modalArray[indexPath.row];
    cell.modal = modal;
    return  cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    topMovieMesViewController *topMovieM = [[topMovieMesViewController alloc] init];
    
    [self.navigationController pushViewController:topMovieM animated:YES];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
